using System.Collections.Generic;
using UnityEngine;

public class AchiManager : MonoBehaviour,ISaveAchiManager
{
    public static AchiManager instance;
    private void Awake()
    {
        instance=this;

        foreach(var i in data)
        {
            oriData.Add(i);
        }
    }

    [Header("成就列表")]
    public List<MyAchiData> data = new List<MyAchiData>();
    [Header("原始列表")]
    public List<MyAchiData> oriData = new List<MyAchiData>();

    public void Load(AchiData data)
    {
        if(data!=null)
        {
            this.data=data.data;
            for(int i=0;i<data.data.Count;i++)
            {
                this.data[i].icon=oriData[i].icon;
            }
        }
        else
        {
            this.data=oriData;
        }
    }

    public void Save(ref AchiData data)
    {
        if(data==null)
        {
            data=new AchiData();
        }
        data.data=this.data;
    }

    //解锁成就
    public void UnLockAchi(string _name)
    {
        //遍历
        foreach(var i in data)
        {
            if(_name==i.achiName)
            {
                if(i.unLock==false)
                {
                    //解锁，跳出完成UI
                    UnLockTip(i);
                }

                break;
            }
        }
    }
    //检测数量成就
    public void CheckNumAchi(string _name)
    {
        //遍历
        foreach (var i in data)
        {
            if (_name==i.achiName)
            {
                if (i.unLock==false)
                {
                    i.currNum+=1;
                    if(i.currNum>=i.targetNum)
                    {
                        //解锁，跳出完成UI
                        UnLockTip(i);
                        break;
                    }
                }
            }
        }
    }
    //解锁提示
    public void UnLockTip(MyAchiData _data)
    {

        Debug.Log("解锁了"+_data.achiName);

        _data.unLock=true;

        MsgAchiUI msg = new MsgAchiUI();
        msg.data=_data;
        EventManager.AddMsg(msg);

        //关闭所有其他音效
        AudManager.instance.CloseAllAud();
        //播放声音
        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["成就完成"], 1);
    }
    //清空获取
    public void Clear()
    {
        for(int i=0;i<data.Count;i++)
        {
            data[i].unLock = false;
            oriData[i].unLock = false;
        }
    }
}

[System.Serializable]
public class MyAchiData
{
    [Header("图片")]
    public Sprite icon;
    [Header("名称")]
    public string achiName;
    [Header("解锁条件")]
    public string des;
    [Header("是否解锁")]
    public bool unLock = false;
    [Header("目标数量")]
    public int targetNum = 30;
    [Header("当前数量")]
    public int currNum = 0;
}